import {
  api,
  util,
  dialog,
} from '../../../../js/util';
import {
  router,pagesInfo
} from '../../../../js/router';
import {
  promotion
} from '../../../../js/modules/market';
import {
  $wuxCountDown,
  $ecCountDown
} from '../../../../components/index'
import authorize from '../../../../js/modules/authorize.js'
import BasePage from '../../../../js/common/base-page'
const app = getApp()
BasePage({
  pageKey:"promotionlist",
  /**
   * 页面的初始数据
   */
  data: {
    pageIndex: 1,
    pageSize: 20,
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    sortField: "Id",
    sortDirect: "desc",
    hasMoreData: true,
    isLoadComplete:false,
    dataType: 1, //状态 1正在进行 0即将开始
    tablist: ['进行中', '未开始', '已结束'],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 35,
    slidewidth: 0,
    list: [
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadList();
  },
  loadList(){
    var that = this;
    var dataType = parseInt(that.data.activeIndex)+1;
    var param = {
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
      sortField: "Reorder",
      sortDirect: "ASC",
      dataType: dataType
    };
    that.setData({
      dataType: dataType
    });
    promotion.loadList(param, function (content) {
      var hasMoreData = content.count > (param.pageIndex) * param.pageSize;
      var index = that.data.list.length;
      var data = content.data;
      //当分页大于1时，累加
      if (param.pageIndex > 1) {
        data = that.data.list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        list: data,
        hasMoreData: hasMoreData,
        isLoadComplete: true
      });
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.pageIndex = 1;
    this.loadList();
    // console.log("onPullDownRefresh");
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    // console.log("onReachBottom");
    if (this.data.hasMoreData) {
      var index = this.data.pageIndex;
      // console.log(index + 1);
      this.data.pageIndex = index + 1;
      this.loadList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let storeInfo = authorize.getEntity();
    let scene="";
    if(storeInfo){
      scene=storeInfo.fkFlag+"_"+storeInfo.fkId+"_"+storeInfo.manager.ManagerId;
    } 
    
    let path = pagesInfo.promotionlist.url + `?scene=${scene}`;
    return {
      title: '促销大全',
      path: path
    }
  },
  /**
   * tab切换
   */
  changeCurrent: function (res) {
    this.setData({
      sliderOffset: res.detail.currentNum.offsetLeft,
      activeIndex: res.detail.currentNum.id,
      pageIndex:1
    });
    this.loadList();
  },
  /**
 * 促销活动跳转
 */
  onSkipPromotion(e) {
    var id = e.currentTarget.dataset.id;
    // app.globalData.product_list.rule_id = id;
    // router.navigate({
    //   path: "productList"
    // })
    router.navigate({
      path:"promotionDetail",
      params:{
        id:id
      }
    })
  },
})